<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .customer-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .img{
   width: 150px;
   height: 100px;
   float: left;
   margin: 5px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>预约客户</span>
    </div>
    <div class="line"></div>
    <customer-form
      class="customer-form"
      @on-success-valid="handleSubmit"
      :title="title"
      :statearr="statearr"
      :paramsAppointime="this.paramsAppointime">
    </customer-form>
    <!-- <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      添加任务
    </Button> -->
    <loss-modal @loss-valid="handleLoss" :row="row"></loss-modal>
    <customer-drawer
      @customer-valid="handleEdit"
      :row="row"
      :detailTitle="title">
    </customer-drawer>
    <appoint-drawer
      :row="row"
      :detailTitle="title"
      @appoint-valid="handleEditAppoint">
    </appoint-drawer>
    <customer-confirm-modal
      :modalState="confirmState"
      :confirmData="confirmData"
      :confirmTitle="title"
      :row="row"
      @customer-confirm-valid="handleConfirm">
    </customer-confirm-modal>
    <appoint-confirm-modal
      :confirmData="confirmData"
      :row="row"
      @appoint-confirm-valid="handleAppointConfirm">
    </appoint-confirm-modal>
    <customer-table
      class="table"
      :tableData="historyData"
      :page="page"
      :pageSize="pageSize"
      @handle-detail="handleClick"
      @handle-click="addLoss"
      :title="title"
      :list="appointmentList"
      :btnTitle="'确认到店'"
      @handle-appoint = 'handleAppoint'
      :loading="loading"
      >
    </customer-table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import CustomerTable from '_c/customer-table'
import CustomerForm from '_c/customer-form'
import CustomerDrawer from '_c/customer-drawer'
import AppointDrawer from '_c/appoint-drawer'
import LossModal from '_c/loss-modal'
import CustomerConfirmModal from '_c/customer-confirm-modal'
import AppointConfirmModal from '_c/appoint-confirm-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    CustomerTable,
    CustomerForm,
    CustomerDrawer,
    LossModal,
    CustomerConfirmModal,
    AppointConfirmModal,
    TablePage,
    AppointDrawer
  },
  data () {
    return {
      page: 1,
      title: '客户管理预约客户',
      rows: 20,
      statearr: '1,2',
      confirmData: {},
      playload: null,
      postData: null,
      loading: true,
      paramsAppointime: '',
      confirmState: false
    }
  },
  computed: {
    ...mapState({
      appointmentList: state => state.appointment.appointmentList,
      dataCount: state => state.appointment.appointmentDataCount,
      historyData: state => state.appointment.appointmentHistoryData,
      pageSize: state => state.appointment.appointmentPageSize,
      row: state => state.appointment.row
    })
  },
  methods: {
    ...mapActions([
      'getAppointmentList',
      'getInsideCustomerNameList',
      'getAppointmentCurrentRow',
      'editAppointmentData',
      'updateCustomerDrawerState',
      'updateCustomerConfirmState',
      'updateLossModalState',
      'updateAppointDrawerState',
      'changeAppointmentPage',
      'changeAppointmentPageSize',
      'updateAppointConfirmState'
    ]),
    handleSubmit (playload) {
      this.loading = true
      this.playload = playload
      this.getAppointmentList({
        state0: 0,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick (row) {
      // console.log(row)
      let newRow = {}
      this.id = row.id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getAppointmentCurrentRow(newRow).then(() => {
        this.updateCustomerDrawerState(true)
      })
    },
    handleAppoint (row) {
      let newRow = {}
      this.id = row.id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getAppointmentCurrentRow(newRow)
      this.updateAppointDrawerState(true)
    },
    handleEditAppoint (playload) {
      // console.log(playload)
      this.confirmData = {
        id: playload.id,
        state: 1,
        appointime: playload.appointime,
        puid: playload.puid,
        remarks: playload.remarks
      }
      this.updateAppointDrawerState(false)
      this.updateAppointConfirmState(true)
    },
    handleEdit (playload, id) {
      this.postData = playload
      if (playload.state === '1' && this.row.ttype === '1') {
        this.confirmData = {
          id: playload.id,
          state: 3,
          comdate: this.row.comdate ? this.$moment(this.row.comdate).format('YYYY-MM-DD HH:mm:ss') : this.$moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          remarks: playload.remarks,
          age: playload.age,
          puid: playload.puid,
          commission: this.row.permoney
        }
      }
      if (playload.state === '1' && this.row.ttype !== '1') {
        this.confirmData = {
          id: playload.id,
          state: 3,
          age: playload.age,
          puid: playload.puid,
          comdate: this.row.comdate ? this.$moment(this.row.comdate).format('YYYY-MM-DD HH:mm:ss') : this.$moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          remarks: playload.remarks
        }
      }
      if (playload.state === '2') {
        this.confirmData = {
          id: playload.id,
          state: 1,
          puid: playload.puid,
          appointime: playload.appointime,
          remarks: playload.remarks
        }
      }
      // console.log(this.confirmData)
      this.updateCustomerDrawerState(false)
      this.updateCustomerConfirmState(true)
    },
    async handleAppointConfirm () {
      this.loading = true
      const resp = await this.editAppointmentData(this.confirmData)
      if (resp.data.state === '000') {
        this.$Message.success('操作成功')
        this.getAppointmentList({
          page: this.page,
          rows: this.rows,
          statearr: '1,2',
          state0: 0,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateAppointConfirmState(false)
      this.postData.customer.resetFields()
    },
    async handleConfirm () {
      this.loading = true
      const resp = await this.editAppointmentData(this.confirmData)
      if (resp.data.state === '000') {
        this.$Message.success('操作成功')
        this.getAppointmentList({
          page: this.page,
          rows: this.rows,
          statearr: '1,2',
          state0: 0,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateCustomerConfirmState(false)
      this.postData.customer.resetFields()
    },
    addLoss (row) {
      // console.log(row)
      let newRow = {}
      this.id = row.id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getAppointmentCurrentRow(newRow)
      this.updateLossModalState(true)
    },
    async handleLoss (playload) {
      this.loading = true
      // console.log(playload)
      const resp = await this.editAppointmentData({
        id: playload.id,
        puid: playload.puid,
        remarks: playload.remarks
      })
      if (resp.data.state === '000') {
        this.$Message.success('用户已流失')
        this.getAppointmentList({
          page: this.page,
          rows: this.rows,
          statearr: '1,2',
          state0: 0,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateLossModalState(false)
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeAppointmentPage(index)
      this.getAppointmentList({
        page: index,
        rows: this.rows,
        statearr: '1,2',
        state0: 0,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeAppointmentPageSize(value)
      this.changePage(this.page)
      this.getAppointmentList({
        rows: value,
        statearr: '1,2',
        state0: 0,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    if (this.$route.params.appointime) {
      this.paramsAppointime = this.$route.params.appointime
      this.getAppointmentList({
        statearr: '1,2',
        state0: 0,
        rows: this.rows,
        appointime: this.$route.params.appointime
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    } else {
      this.getAppointmentList({
        statearr: '1,2',
        state0: 0,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
    this.getInsideCustomerNameList({})
  }
}
</script>
